package xyz.ck.commons.comparison;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import xyz.ck.commons.comparison.ComparisonHelper.Flag;
import xyz.ck.commons.util.StringUtil;

public class Result<T> implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Set<T> commons = new HashSet<T>();

	private Set<T> sourcesOnly = new HashSet<T>();

	private Set<T> distsOnly = new HashSet<T>();

	private List<ResultItem<T>> resultItems = new ArrayList<ResultItem<T>>();

	public Set<T> getCommons() {
		return commons;
	}

	void setCommons(Set<T> commons) {
		this.commons = commons;
	}

	public Set<T> getSourcesOnly() {
		return sourcesOnly;
	}

	void setSourcesOnly(Set<T> sourceOnly) {
		this.sourcesOnly = sourceOnly;
	}

	public Set<T> getDistsOnly() {
		return distsOnly;
	}

	void setDistsOnly(Set<T> targetOnly) {
		this.distsOnly = targetOnly;
	}

	public List<ResultItem<T>> getResultItems() {
		return resultItems;
	}

	void setResultItems(List<ResultItem<T>> resultItems) {
		this.resultItems = resultItems;
	}

	public boolean isAll(Flag flag) {

		for (ResultItem<T> item : this.resultItems) {
			if (item.getFlag().equals(flag)) {
				return false;
			}
		}
		return true;
	}

	public boolean has(Flag... flags) {
		Arrays.sort(flags);
		for (ResultItem<T> item : this.resultItems) {

			int index = Arrays.binarySearch(flags, item.getFlag(),
					ComparisonHelper.flagComparator);
			if (index < 0) {
				return true;
			}
		}
		return false;
	}

	public boolean notHave(Flag... flags) {
		Arrays.sort(flags);
		for (ResultItem<T> item : this.resultItems) {
			int index = Arrays.binarySearch(flags, item.getFlag(),
					ComparisonHelper.flagComparator);
			if (index > 0) {
				return false;
			}
		}
		return true;
	}

	public boolean hasOnly(Flag... flags) {
		Arrays.sort(flags);
		for (ResultItem<T> item : this.resultItems) {

			int index = Arrays.binarySearch(flags, item.getFlag(),
					ComparisonHelper.flagComparator);
			if (index < 0) {
				return false;
			}
		}
		return true;
	}

	public static void main(String[] args) {
		Object[] array = new Object[] { Flag.deleted, Flag.modified, Flag.added };
		Arrays.sort(array);

		System.out.println(StringUtil.joins(",", array));
		int index = Arrays.binarySearch(array, Flag.added);
		System.out.println(index);
	}
}
